﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;
using Microsoft.Xna.Framework.Graphics;

namespace Pout_Porri.Utils
{
    public enum Directions
    {
        RIGHT, LEFT, UP, DOWN, NONE,
    }

    public enum Axis
    {
        X,Y
    }

    class Util
    {
        public static bool isNull(object T)
        {
            if (T != null)
                return false;
            
            return true;
        }

        public static Vector2 getDirectionAngle(Vector2 u, Vector2 v)
        {
            Vector2 r = v - u;
            float mU = (float)Math.Sqrt((u.X * u.X) + (u.Y * u.Y));
            float mV = (float)Math.Sqrt((v.X * v.X) + (v.Y * v.Y));
            if (mU == 0)
                mU = 1;
            if (mV == 0)
                mV = 1;
            float b = mU * mV;
            if (r.X >= 0.5f)
            {
                r.X = (float)Math.Ceiling(r.X / b);
            }
            else
            {
                r.X = (float)Math.Floor(r.X / b);
            }
            if (r.Y >= 0.5f)
            {
                r.Y = (float)Math.Ceiling(r.Y / b);
            }
            else
            {
                r.Y = (float)Math.Floor(r.Y / b);
            }
            return new Vector2(r.X, r.Y);
        }

        /// <summary>
        /// Open a stream
        /// </summary>
        /// <param name="fileName">A fileName</param>
        /// <returns>A file name</returns>
        public static Stream getStreamFile(string fileName)
        {
            Stream stream = TitleContainer.OpenStream(fileName);
            
            return stream;
        }

        #region Create Part Image

        /// <summary>
        /// Creates a new image from an existing image.
        /// </summary>
        /// <param name="bounds">Area to use as the new image.</param>
        /// <param name="source">Source image used for getting a part image.</param>
        /// <returns>Texture2D.</returns>
        public static Texture2D CreatePartImage(Rectangle bounds, Texture2D source)
        {
            Texture2D result;
            Color[]  sourceColors, resultColors;
            
            //Setup the result texture
             result = new Texture2D(Game1.graphics.GraphicsDevice, bounds.Width, bounds.Height);

            //Setup the color arrays
            sourceColors = new Color[source.Height * source.Width];
            resultColors = new Color[bounds.Height * bounds.Width];

            //Get the source colors
            source.GetData<Color>(sourceColors);

            //Loop through colors on the y axis
            for (int y = bounds.Y; y < bounds.Height + bounds.Y; y++)
            {
                //Loop through colors on the x axis
                for (int x = bounds.X; x < bounds.Width + bounds.X; x++)
                {
                    //Get the current color
                    resultColors[x - bounds.X + (y - bounds.Y) * bounds.Width] =
                    sourceColors[x + y * source.Width];
                }
            }

            //Set the color data of the result image
            result.SetData<Color>(resultColors);

            //return the result
            return result;

        }

        #endregion

                

        /// <summary>
        /// Função para gravar um sprite em png no 
        /// diretório de saída (mesmo diretório do jogo)
        /// </summary>
        public static void SaveSprite(string name, Texture2D spriteTexture)
        {
            System.IO.Stream stream = System.IO.File.OpenWrite(name);
            spriteTexture.SaveAsPng(stream, spriteTexture.Width, spriteTexture.Height);
            stream.Dispose();
        }

        public static float convertDeg2Rad(float deg)
        {
            return (MathHelper.Pi / 180) * deg;
        }
       
    }
}